<template>
  <el-dialog
    :title="!dataForm.gsmLiveId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动标题" prop="gsmLiveTitle">
      <el-input v-model="dataForm.gsmLiveTitle" placeholder="活动标题"></el-input>
    </el-form-item>
    <el-form-item label="封面图片" prop="gsmLiveImg">
      <el-input v-model="dataForm.gsmLiveImg" placeholder="封面图片"></el-input>
    </el-form-item>
    <el-form-item label="直播活动开始时间" prop="gsmLiveStartTime">
      <el-input v-model="dataForm.gsmLiveStartTime" placeholder="直播活动开始时间"></el-input>
    </el-form-item>
    <el-form-item label="直播状态" prop="gsmLiveStatus">
      <el-input v-model="dataForm.gsmLiveStatus" placeholder="直播状态"></el-input>
    </el-form-item>
    <el-form-item label="推流地址" prop="gsmLivePushUrl">
      <el-input v-model="dataForm.gsmLivePushUrl" placeholder="推流地址"></el-input>
    </el-form-item>
    <el-form-item label="播流地址" prop="gsmLivePullUrl">
      <el-input v-model="dataForm.gsmLivePullUrl" placeholder="播流地址"></el-input>
    </el-form-item>
    <el-form-item label="播主名称" prop="gsmLiveUp">
      <el-input v-model="dataForm.gsmLiveUp" placeholder="播主名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gsmLiveId: 0,
          gsmLiveTitle: '',
          gsmLiveImg: '',
          gsmLiveStartTime: '',
          gsmLiveStatus: '',
          gsmLivePushUrl: '',
          gsmLivePullUrl: '',
          gsmLiveUp: ''
        },
        dataRule: {
          gsmLiveTitle: [
            { required: true, message: '活动标题不能为空', trigger: 'blur' }
          ],
          gsmLiveImg: [
            { required: true, message: '封面图片不能为空', trigger: 'blur' }
          ],
          gsmLiveStartTime: [
            { required: true, message: '直播活动开始时间不能为空', trigger: 'blur' }
          ],
          gsmLiveStatus: [
            { required: true, message: '直播状态不能为空', trigger: 'blur' }
          ],
          gsmLivePushUrl: [
            { required: true, message: '推流地址不能为空', trigger: 'blur' }
          ],
          gsmLivePullUrl: [
            { required: true, message: '播流地址不能为空', trigger: 'blur' }
          ],
          gsmLiveUp: [
            { required: true, message: '播主名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gsmLiveId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gsmLiveId) {
            this.$http({
              url: this.$http.adornUrl(`/live/live/info/${this.dataForm.gsmLiveId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.gsmLiveTitle = data.live.gsmLiveTitle
                this.dataForm.gsmLiveImg = data.live.gsmLiveImg
                this.dataForm.gsmLiveStartTime = data.live.gsmLiveStartTime
                this.dataForm.gsmLiveStatus = data.live.gsmLiveStatus
                this.dataForm.gsmLivePushUrl = data.live.gsmLivePushUrl
                this.dataForm.gsmLivePullUrl = data.live.gsmLivePullUrl
                this.dataForm.gsmLiveUp = data.live.gsmLiveUp
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/live/live/${!this.dataForm.gsmLiveId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gsmLiveId': this.dataForm.gsmLiveId || undefined,
                'gsmLiveTitle': this.dataForm.gsmLiveTitle,
                'gsmLiveImg': this.dataForm.gsmLiveImg,
                'gsmLiveStartTime': this.dataForm.gsmLiveStartTime,
                'gsmLiveStatus': this.dataForm.gsmLiveStatus,
                'gsmLivePushUrl': this.dataForm.gsmLivePushUrl,
                'gsmLivePullUrl': this.dataForm.gsmLivePullUrl,
                'gsmLiveUp': this.dataForm.gsmLiveUp
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
